package cn.lyf.controller;

<<<<<<< HEAD
=======
import cn.lyf.dao.AnswerDao;
>>>>>>> a254f66694e58548627c0548f6e222a03570bbfd
import cn.lyf.entity.Blog;
import cn.lyf.entity.BlogType;
import cn.lyf.entity.User;
import cn.lyf.respjson.RespJson;
<<<<<<< HEAD
import cn.lyf.service.CommentService;
=======
import cn.lyf.service.AnswerService;
>>>>>>> a254f66694e58548627c0548f6e222a03570bbfd
import cn.lyf.service.BlogService;
import cn.lyf.service.BlogTypeService;
import cn.lyf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@RequestMapping("/api/public")
public class PublicController {
    @Autowired
    UserService userService;

    @Autowired
    BlogTypeService blogTypeService;

    @Autowired
    PasswordEncoder passwordEncoder;

    @Autowired
    BlogService blogService;

    @Autowired
<<<<<<< HEAD
    CommentService commentService;
=======
    AnswerService answerService;
>>>>>>> a254f66694e58548627c0548f6e222a03570bbfd

    @RequestMapping("/go2Login")
    public String go2Login(){
        return "login/login";
    }

    @RequestMapping("/go2Index")
    public String go2Index(){
        return "index";
    }

    @RequestMapping("/go2AddBlogger")
    public String go2AddBlogger(){
        return "user/addBlogger";
    }

    @RequestMapping("/go2AddAdm")
    public String go2AddAdm(){
        return "user/addAdm";
    }

    //注册用户
    @PostMapping("/addUser")
    @ResponseBody
    public Map<String,Object> addUser(@RequestBody User user){
        System.out.println("user=="+user);
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        userService.add(user);
        return RespJson.pack(200,"注册成功",null);
    }

<<<<<<< HEAD


    //查找所有博客类型
    @GetMapping("/findAllBlogTypes")
=======
    //添加博客类别
    @RequestMapping("/addBlogType")
    @ResponseBody
    public Map<String,Object> add( BlogType blogType){
        System.out.println("blogtype=="+blogType);
        blogType.setNum(0);
        blogTypeService.add(blogType);
        return RespJson.pack(200,"",null);
    }

    //查找所有博客类型
    @RequestMapping("/findAllBlogTypes")
>>>>>>> a254f66694e58548627c0548f6e222a03570bbfd
    @ResponseBody
    public Map<String,Object> findAllBlogType(){
       return RespJson.pack(200,"",blogTypeService.findAll());
    }


<<<<<<< HEAD
=======
    @RequestMapping("/go2AddBlogType")
    public String go2Add(){
        return "blogtype/addBlogType";
    }

>>>>>>> a254f66694e58548627c0548f6e222a03570bbfd

   //根据id查找类型
   @GetMapping("/findTypeById/{id}")
   @ResponseBody
    public Map<String,Object> findTypeById(@PathVariable("id")String id){
      return RespJson.pack(200,"",blogTypeService.findById(id));
   }

   //根据博客类别id查找对应所有博客
    @GetMapping("/findBlogsByTypeId/{typeid}")
    @ResponseBody
    public Map<String,Object> findBlogsByTypeId(@PathVariable("typeid")String typeid){
        return RespJson.pack(200,"",blogService.findBlogsByTypeId(typeid));
    }

    //跳转到博客文章的页面
    @GetMapping("/go2ReadBlog/{blogid}")
    public String go2ReadBlog(@PathVariable("blogid")String id){
        return "blog/go2ReadBlog";
    }

    //添加阅读量
<<<<<<< HEAD
    @GetMapping("/addReadNum/{blogid}")
=======
    @RequestMapping("/addReadNum/{blogid}")
>>>>>>> a254f66694e58548627c0548f6e222a03570bbfd
    @ResponseBody
    public Map<String,Object> addReadnum(@PathVariable("blogid")String id){
        blogService.addReadnum(id);
        return RespJson.pack(200,"",null);
    }

    //根据博客id查找博客
<<<<<<< HEAD
    @GetMapping("/findOneBlog/{blogid}")
=======
    @RequestMapping("/findOneBlog/{blogid}")
>>>>>>> a254f66694e58548627c0548f6e222a03570bbfd
    @ResponseBody
    public Map<String,Object> findOneBlog(@PathVariable("blogid")String blogid){
        return RespJson.pack(200,"",blogService.findOneBlog(blogid));
    }

    //跳转到搜索页面
    @RequestMapping("/go2Search")
    public String go2Search(){
        return "blog/go2Search";
    }

    //根据标题进行模糊查找
    @PostMapping("/findBlogsByTitle")
    @ResponseBody
    public Map<String,Object> findBlogsByTitle(@RequestBody Blog blog){
<<<<<<< HEAD
        System.out.println("blog=="+blog);
        System.out.println("blogs=="+blogService.findBlogsByTitle(blog.getTitle()));
=======
>>>>>>> a254f66694e58548627c0548f6e222a03570bbfd
        return RespJson.pack(200,"",blogService.findBlogsByTitle(blog.getTitle()));
    }

    //查看某篇博客的所有评论
<<<<<<< HEAD
    @GetMapping("/findCommentsByBlogId/{blogid}")
    @ResponseBody
    public Map<String,Object> findAnswersByBlogId(@PathVariable("blogid")String blogid){
        return RespJson.pack(200,"", commentService.findCommentsByBlogId(blogid));
=======
    @GetMapping("/findAnswersByBlogId/{blogid}")
    @ResponseBody
    public Map<String,Object> findAnswersByBlogId(@PathVariable("blogid")String blogid){
        return RespJson.pack(200,"",answerService.findAnswersByBlogId(blogid));
>>>>>>> a254f66694e58548627c0548f6e222a03570bbfd
    }




}
